package com.zzyl.config;

import com.aliyun.iot20180120.Client;
import com.aliyun.teaopenapi.models.Config;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.zzyl.properties.AliIoTConfigProperties;
import com.zzyl.properties.HuaweiIoTConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class IotClientConfig {

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    @Autowired
    private HuaweiIoTConfigProperties hProperties;


    @Bean
    public Client getClinet() throws Exception {
        Config config = new Config();
        config.accessKeyId = aliIoTConfigProperties.getAccessKeyId();
        config.accessKeySecret = aliIoTConfigProperties.getAccessKeySecret();
        // 您的可用区ID 默认上海
        config.regionId = aliIoTConfigProperties.getRegionId();
        return new Client(config);
    }

    @Bean
    public IoTDAClient getHuaweiClient() {
        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(hProperties.getAccessKeyId())
                .withSk(hProperties.getSecretAccessKey())
                .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
                .withProjectId(hProperties.getProjectId());

        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(new Region(hProperties.getRegionId(), hProperties.getEndpoint()))
                // .withHttpConfig(new HttpConfig().withIgnoreSSLVerification(true))
                .build();
        return client;
    }
}